<?php
namespace app\admin\controller;
use app\common\model\Menu as MenuModel;
use app\common\model\Manager as ManagerModel;

class Manager extends Base
{   
    public function __construct () {
        parent::__construct();
    }
    /* 
     列表
     */
    public function list(){
        $where = [
            'mall_id' => $this->manager['mall_id']
        ];
        $page = max(input('page'),1);
        $size = 20;
        $managerList = $this->managerModel->getPage($where,$page,$size);
        $total = $this->managerModel->getCount($where);
        if(!$managerList) {
            return returnJson(1,'');
        }
        foreach($managerList as $k=>$v) {
            unset($managerList[$k]['password']);
            $menus = explode(',',$v['menu']);
            foreach($menus as $kk=>$vv) {
                if($vv=='') {
                    continue;
                }
                $menus[$kk] = (int)$vv;
            }
            $managerList[$k]['menu'] = $menus;
        }
        return returnJson(0,'',[
            'list' => $managerList,
            'size' => $size,
            'total' => $total
        ]);
    }

    /* 新增管理员/修改管理员 */
    public function edit () {
        if(request()->isPost()) {
            $input = input();
            if(!isset($input['name'])) {
                return returnJson(1,'请填写完整');
            }
            $data = array(
                'name' => $input['name'],
                'mall_id' => $this->manager['mall_id'],
                'menu' => implode(',',$input['menu'])
            );
            if($input['password']!='') {
                if(checkPassword($input['password'])<3) {
                    return returnJson(1,'密码过于简单，建议使用数字字母组合密码');
                }
                $data['password'] = createPassword($input['password']);
            }
            if($input['id']) {
                $data['id'] = $input['id'];
            }

            $nameOld = $this->managerModel->getOne(['name'=>$input['name']]);
            if($nameOld&&$nameOld['id']!=$input['id']) {
                return returnJson(1,'用户名已存在');
            }

            if($data['id']) {
                $res = $this->managerModel->isUpdate(true)->save($data);
            }else{
                $res = $this->managerModel->isUpdate(false)->save($data);
            }
            if($res) {
                return returnJson(0,'操作成功');
            }
            return returnJson(1,'网络异常，请重试');
        }
    }

    /* 删除 */
    public function delete () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        $manager = $this->managerModel->getOne(['id'=>$input['id']]);
        if(!$manager) {
            return returnJson(1,'管理员不存在');
        }
        if($this->manager['is_super']!=1) {
            return returnJson(1,'只有超级管理员可以删除');
        }
        if($manager['is_super']) {
            return returnJson(1,'超级管理员无法删除');
        }
        $res = $this->managerModel->where(['id'=>$input['id']])->delete();
        if($res) {
            return returnJson(0,'已删除');
        }else{
            return returnJson(1,'删除失败，请重试');
        }
    }

    /* 分公司管理员删除 */
    public function malldelete () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        $manager = $this->managerModel->getOne(['id'=>$input['id']]);
        if(!$manager) {
            return returnJson(1,'管理员不存在');
        }
        if($manager['mall_id']==0) {
            return returnJson(1,'管理员无法删除');
        }
        if($this->manager['mall_id']!=0) {
            if($manager['mall_id']!=$this->manager['mall_id']) {
                return returnJson(1,'管理员无法删除');
            }
        }
        $res = $this->managerModel->where(['id'=>$input['id']])->delete();
        if($res) {
            return returnJson(0,'已删除');
        }else{
            return returnJson(1,'删除失败，请重试');
        }
    }

    /* 子公司列表 */
    public function malllist () {
        $where = [
            'mall_id' => [
                'gt',0
            ]
        ];
        $input = input();
        if($input['mall_id']) {
            $where['mall_id'] = $input['mall_id'];
        }
        if($input['mall_name']) {
            $mallList = $this->mallModel->where('name','like',"%{$input['mall_name']}%")->column('id');
            if(!$mallList) {
                $mallList = [-1];
            }
            $where['mall_id'] = [
                'in',$mallList
            ];
        }
        $page = max(input('page'),1);
        $size = 20;
        $managerList = $this->managerModel->getPage($where,$page,$size);
        $total = $this->managerModel->getCount($where);
        if(!$managerList) {
            return returnJson(1,'');
        }
        foreach($managerList as $k=>$v) {
            unset($managerList[$k]['password']);
            $managerList[$k]['mallinfo'] = $this->mallModel->getOne(['id'=>$v['mall_id']]);
        }
        return returnJson(0,'',[
            'list' => $managerList,
            'size' => $size,
            'total' => $total
        ]);
    }

    /* 子公司管理员 */
    public function malledit () {
        if(request()->isPost()) {
            $input = input();
            if(!isset($input['name'])) {
                return returnJson(1,'请填写管理员名称');
            }
            if(!isset($input['mall_id'])) {
                return returnJson(1,'请选择子公司');
            }
            $data = array(
                'name' => $input['name'],
                'mall_id' => $input['mall_id'],
                'is_super' => $input['is_super']
            );
            if($input['password']!='') {
                if(checkPassword($input['password'])<3) {
                    return returnJson(1,'密码过于简单，建议使用数字字母组合密码');
                }
                $data['password'] = createPassword($input['password']);
            }
            if($input['id']) {
                $data['id'] = $input['id'];
            }
            $nameOld = $this->managerModel->getOne(['name'=>$input['name']]);
            if($nameOld&&$nameOld['id']!=$input['id']) {
                return returnJson(1,'用户名已存在');
            }
            if($data['id']) {
                $res = $this->managerModel->isUpdate(true)->save($data);
            }else{
                $res = $this->managerModel->isUpdate(false)->save($data);
            }
            if($res) {
                return returnJson(0,'操作成功');
            }
            return returnJson(1,'网络异常，请重试');
        }
    }

    /* 权限组 */
    public function menuList () {
        $where = [
            'status' => 1,
            'level' => 1
        ];
        if($this->manager['mall_id']==0) {
            $where['is_admin'] = 1;
        }else{
            $where['is_mall'] = 1;
        }
        $menus = MenuModel::getAll($where,'ord desc,id asc');
        foreach($menus as $k=>$v) {
            $subwhere = [
                'status' => 1,
                'is_limit' => 1,
                'controller' => $v['controller']
            ];
            if($this->manager['mall_id']==0) {
                $subwhere['is_admin'] = 1;
            }else{
                $subwhere['is_mall'] = 1;
            }
            $subList = MenuModel::getAll($subwhere,'ord desc,id asc');
            if($subList) {
                $menus[$k]['children'] = $subList;
            }
        }
        return returnJson(0,'',$menus);
    }
}
